<#import "violations.ftl" as violations/>
<#import "xml.ftl" as xml/>

<#-- Iterate over the tree structure -->
<#macro treeIterate tree>
	<ul>
		<#if (tree.content.name == "level1")>
			<li><span onclick="$('${tree.content.hashCode()}').toggle()"
					style="text-decoration:underline; cursor:pointer">${tree.content.name}</span>
				<#if (tree.metaContent["pagenum"]??)>
					[Page(s):<#list tree.metaContent["pagenum"] as pagenum> ${pagenum}</#list>]
				</#if>
			</li>
			<div id="${tree.content.hashCode()}" style="display:none">
				<#list tree.children as child>
					<@treeIterate child/>
				</#list>
			</div>
		<#else>
			<li>${tree.content.name}
				<#if (tree.metaContent["pagenum"]??)>
					[Page(s):<#list tree.metaContent["pagenum"] as pagenum> ${pagenum}</#list>]
				</#if>
			</li>
			<#list tree.children as child>
					<@treeIterate child/>
			</#list>
		</#if>
	</ul>
</#macro>

<#-- Print out a list of violations for a given document -->
<#macro documentViolationList doc>
	<#if report.documentViolations.get(doc)??>
		<@xml.docHeading doc "h3"/>
		<#assign violationList = report.documentViolations.get(doc)/>
		<#if (violationList?size > 0)>
			<ul>
				<#list violationList as violation>
					<@violations.render violation/>
				</#list>
			</ul>
		<#else>
			<em>Correctly validated against DTD</em>
		</#if>
	</#if>
</#macro>

<#-- Print out a list of violations for a given list of documents -->
<#macro docsElementViolationList doc>
	<@xml.docHeading doc "h3"/>
	<ul>
		<@elementViolations doc.rootElement/>
	</ul>
</#macro>

<#-- Print out a list of violations for an element and its children -->
<#macro elementViolations element>
	<#if report.elementViolations.get(element)??>
		<#assign violationList = report.elementViolations.get(element)/>
		<#if (violationList?size > 0)>
			<#list violationList as violation>
				<@violations.render violation elementPath(element, "")/>
			</#list>
		</#if>
	</#if>
	<#list element.children as child>
		<@elementViolations child/>
	</#list>
</#macro>

<#-- Print out an element's full path -->
<#function elementPath element path>
    <#if element.parent??>
    	<#return elementPath(element.parent, (element.name + "/" + path))/>
    <#else>
    	<#return path/>
    </#if>
</#function>

<#-- Render the metadata section -->
<#macro metadata>
	<#if report.modelMap["dcMetadata"]["title"]?? && (report.modelMap["dcMetadata"]["title"]?length > 0)>
		<div><b>Title</b> -- <em>${report.modelMap["dcMetadata"]["title"].get(0).text}</em></div>
	</#if>
	<#if report.modelMap["dcMetadata"]["author"]?? && (report.modelMap["dcMetadata"]["author"]?length > 0)>
		<div><b>Author</b> -- <em>${report.modelMap["dcMetadata"]["author"].get(0).text}</em></div>
	</#if>
	<#if report.modelMap["dcMetadata"]["publisher"]?? && (report.modelMap["dcMetadata"]["publisher"]?length > 0)>
		<div><b>Publisher</b> -- <em>${report.modelMap["dcMetadata"]["publisher"].get(0).text}</em></div>
	</#if>
	<#if report.modelMap["dcMetadata"]["isbn"]?? && (report.modelMap["dcMetadata"]["isbn"]?length > 0)>
		<div><b>ISBN</b> -- <em>${report.modelMap["dcMetadata"]["isbn"].get(0).text}</em></div>
	</#if>
	<@metadataMap report.modelMap["xMetadata"], xMetadataKeys,
		["title", "author", "publisher", "isbn"]>NIMAS Metadata</@metadataMap>
	<@metadataMap report.modelMap["dcMetadata"], dcMetadataKeys,
		["title", "author", "publisher", "isbn"]>DAISY Metadata</@metadataMap>
</#macro>

<#-- Render a metadata map (NIMAS or DAISY) -->
<#macro metadataMap map, keys, excluded>
	<#assign id=map.hashCode()?c/>
	<div id="maplink_${id}" class="detailsbutton"
		onclick="$('maplink_${id}').toggle(); $('mapcontent_${id}').toggle();">
		<li><#nested/></li>
	</div>
	<div id="mapcontent_${id}" class="contentbox" style="display:none">
		<span class="detailsbutton" onclick="$('maplink_${id}').toggle();
			$('mapcontent_${id}').toggle();"><#nested/></span>
		<#list keys as mapkey>
			<#if !excluded?seq_contains(mapkey)>
				<ul>
				<#list map[mapkey] as element>
					<li><b>${element.name}</b> --
					 <#if (element.attributes?length > 0)>
					 	<#list element.attributes as attribute>
					 		(${attribute.name} : ${attribute.value})
			 			</#list>
					 </#if>
					 <em>${element.text}</em>
  					 <#if report.elementViolations.get(element)??>
	 					 <ul><@elementViolations element/></ul>
					</#if>
					</li>
				</#list>
				</ul>
			</#if>
		</#list>
	</div>
</#macro>

<#macro packageContents>
	<#assign files = {"OPF" : "opf", "XML" : "xml", "PDF" : "pdf"} />
	<#list files?keys as filekey>
		<ul>
			<b>${filekey}</b>
			<#list report.modelMap["files"][files[filekey]]![] as file>
				<li><a href="${file}">${file?substring(basePath?length)}</a></li>
	  		 </#list>
		</ul>
	</#list>
	<li id="imagesLink" class="detailsbutton"
		onclick="$('imagesLink').toggle(); $('imagesContent').toggle()">
		Number of images: <#if report.modelMap["files"]["img"]??>${report.modelMap["files"]["img"]?size}<#else>0</#if></li>
	<div id="imagesContent" class="contentbox" style="display:none"
		onclick="$('imagesLink').toggle(); $('imagesContent').toggle()">
		<b>Image Files</b>
		<ul>
		<#list report.modelMap["files"]["img"]![] as file>
			<#if file??>
				<li><a href="${file}">${file?substring(basePath?length)}</a></li>
			</#if>
	  	</#list>
	  	</ul>
	</div>
	<li id="otherLink" class="detailsbutton"
		onclick="$('otherLink').toggle(); $('otherContent').toggle()">
		Number of other files: <#if report.modelMap["files"]["other"]??>${report.modelMap["files"]["other"]?size}<#else>0</#if></li>
	<div id="otherContent" class="contentbox" style="display:none"
		onclick="$('otherLink').toggle(); $('otherContent').toggle()">
		<b>Other Files In Package</b>
		<ul>
		<#list report.modelMap["files"]["other"]![] as file>
			<li><a href="${file}">${file?substring(basePath?length)}</a></li>
	  	</#list>
	  	</ul>
	</div>
</#macro>

<#-- Dump all known properties on the given object -->
<#macro debugObject object>
	<#if object??>
		<#attempt>
			<ul>
				<#list object?keys as key>
					<li>${key}: {object[key]}</li>
				</#list>
			</ul>
		<#recover>
			Could not parse object as hash. Try using a different BeanWrapper.
		</#attempt>
	<#else>
		Object is null!
	</#if>
</#macro>
<#-- _______________ HTML BEGINS HERE ________________ -->
<html>
<head>
	<title>${report.name} Validation Report</title>
	<script src="${resourcePath}/prototype.js"/>
	<script src="${resourcePath}/scriptaculous.js"/>
	<script src="${resourcePath}/nimasreport.js"/>
	<script src="${resourcePath}/litbox.js"/>
	<link rel="stylesheet" type="text/css" href="${resourcePath}/litbox.css"/>

	<style type="text/css">
		.ERROR {
			list-style-image: url(${resourcePath + "/error.png"});
			margin-bottom: 7px;
		}
		.WARNING {
			list-style-image: url(${resourcePath + "/warning.png"});
			margin-bottom: 7px;
		}
		.INFO {
			list-style-image: url(${resourcePath + "/info.png"});
			margin-bottom: 7px;
		}
		.noicon {
			list-style-image: none;
		}
		.button {
			border:solid gray 1px;
			cursor:pointer;
			margin:3px;
			margin-top: 7px;
			padding: 1px;
		}
		.detailsbutton {
			cursor:pointer;
			text-decoration: underline;
			margin:3px;
			margin-top: 7px;
			padding: 1px;
			list-style-image: url(${resourcePath + "/details.png"});
		}
		.icon {
			cursor:pointer;
			margin:3px;
			margin-top: 4px;
		}
		.contentbox {
		 	border:solid black 1px;
			cursor:pointer;
			margin:8px;
			padding:8px;
		}
		.summarybox {
		 	border:solid black 1px;
			margin:5px;
			padding:5px;
		}
		.shaded {
			background-color: #DDE;
		}
		.errorSummary {
			font-weight: bold;
			color: red;
		}
		.sourcetable {
			border-collapse: collapse;
			width: 100%;
		}
		.iconbar {
			position: fixed;
			top: 0px;
			right: 0px;
		}
	</style>
</head>
<body>
	<div><h1>Summary</h1><span class="iconbar"><img src="${resourcePath}/printer1.png" alt="Print Document" class="icon" onclick="printPage();"/>
													<img src="${resourcePath}/help.png" alt="Frequently Asked Questions" class="icon" onclick="help('${resourcePath}');"/>
													<img src="${resourcePath}/shrink.png" alt="Hide Details" class="icon" onclick="collapseAll();"/></span></div>
	<a name="summary"/>
	<div>
		<table width="80%">
		<tr>
				<td>
					<h2>Metadata</h2>
					<a name="metadata"/>
					<div>
						<@metadata/>
					</div>
					<hr width="50%" align="left"/>
					<h2>Package Contents</h2>
					<a name="rawfiles"/>
					<div>
						<@packageContents/>
				    </div>
			    </td>
		    <td valign="top">
			    <h2>Statistics</h2>
			    <a name="statistics"/>
			    <div>
			    	<h3>Problems</h3>
			    	<table class="summarybox">
			    		<#if (report.modelMap["totalExceptions"] > 0)>
				    		<tr><td><a href="#applicationerrors">Validator Errors:</a></td>
				    			<td>${report.modelMap["totalExceptions"]}</td></tr>
		    			</#if>
						<tr><td><h4>Total Found:</h4></td><td><h4>${report.modelMap["totalErrors"]}</h4></td></tr>
						<tr <#if (report.modelMap["totalErrorErrors"]?size > 0)>class="errorSummary"</#if>><td><li class="ERROR">Critical:</li></td>
							<td>${report.modelMap["totalErrorErrors"]}</td></tr>
						<tr><td><li class="WARNING">Warnings:</li></td>
							<td>${report.modelMap["totalWarningErrors"]}</td></tr>
						<tr><td><li class="INFO">Informational:</li></td>
							<td>${report.modelMap["totalInfoErrors"]}</td></tr>
					</table>
					<div><li class="detailsbutton" onclick="$('errordetails').toggle(); $('errordetails').scrollTo(true);">Details</li></div>

					<h3>Document Structure</h3>
					<table class="summarybox">
						<tr><td>Number of Sections:</td>
							<td>${report.modelMap["totalSections"]}</td></tr>
						<tr><td>Number of Pages:</td>
							<td>${report.modelMap["totalPages"]}</td></tr>
					</table>
					<div><li class="detailsbutton" onclick="$('docstructure').toggle(); $('docstructure').scrollTo(true);">Details</li></div>

				</div>
			</td>
		</tr>
		</table>
    </div>
	<hr/>
	<div id="docstructure" style="display:none">
		<a name="docstructure"/>
		<h2>XML Document Structure</h2>
		<a name="headings"/>
		<div>
			<#assign docMap = report.modelMap["headings"]/>
			<@xml.docHeading xmlDoc "h3"/>
			<div>
				<@treeIterate docMap[xmlDoc.baseURI]/>
			</div>
		</div>
	</div>

	<div id="errordetails" style="display:none">
		<#if (report.modelMap["totalExceptions"] > 0)>
			<h2>ApplicationErrors</h2>
			See output logs for details.
			<a name="applicationerrors"/>
			<div>
				<ul>
				<#list report.exceptions?keys as exception>
					<li>${exception} - ${report.exceptions[exception].message}</li>
				</#list>
				</ul>
			</div>
		</#if>

		<h2>Element Violations</h2>
		<a name="elementviolations"/>
		<div>
			<@docsElementViolationList opfDoc/>
			<@docsElementViolationList xmlDoc/>
		</div>

		<h2>Document Violations</h2>
		<a name="documentviolations"/>
		<div>
			<@documentViolationList opfDoc/>
			<@documentViolationList xmlDoc/>
		</div>

	</div>
</body>
</html>